﻿using System;

namespace UniRx.Operators
{
	// Token: 0x02000311 RID: 785
	internal class SynchronizeObservable<T> : OperatorObservableBase<T>
	{
		// Token: 0x06001075 RID: 4213 RVA: 0x0004BEB8 File Offset: 0x0004A2B8
		public SynchronizeObservable(IObservable<T> source, object gate) : base(source.IsRequiredSubscribeOnCurrentThread<T>())
		{
			this.source = source;
			this.gate = gate;
		}

		// Token: 0x06001076 RID: 4214 RVA: 0x0004BED4 File Offset: 0x0004A2D4
		protected override IDisposable SubscribeCore(IObserver<T> observer, IDisposable cancel)
		{
			return this.source.Subscribe(new SynchronizeObservable<T>.Synchronize(this, observer, cancel));
		}

		// Token: 0x040009AB RID: 2475
		private readonly IObservable<T> source;

		// Token: 0x040009AC RID: 2476
		private readonly object gate;

		// Token: 0x02000312 RID: 786
		private class Synchronize : OperatorObserverBase<T, T>
		{
			// Token: 0x06001077 RID: 4215 RVA: 0x0004BEE9 File Offset: 0x0004A2E9
			public Synchronize(SynchronizeObservable<T> parent, IObserver<T> observer, IDisposable cancel) : base(observer, cancel)
			{
				this.parent = parent;
			}

			// Token: 0x06001078 RID: 4216 RVA: 0x0004BEFC File Offset: 0x0004A2FC
			public override void OnNext(T value)
			{
				object gate = this.parent.gate;
				lock (gate)
				{
					this.observer.OnNext(value);
				}
			}

			// Token: 0x06001079 RID: 4217 RVA: 0x0004BF48 File Offset: 0x0004A348
			public override void OnError(Exception error)
			{
				object gate = this.parent.gate;
				lock (gate)
				{
					try
					{
						this.observer.OnError(error);
					}
					finally
					{
						base.Dispose();
					}
				}
			}

			// Token: 0x0600107A RID: 4218 RVA: 0x0004BFAC File Offset: 0x0004A3AC
			public override void OnCompleted()
			{
				object gate = this.parent.gate;
				lock (gate)
				{
					try
					{
						this.observer.OnCompleted();
					}
					finally
					{
						base.Dispose();
					}
				}
			}

			// Token: 0x040009AD RID: 2477
			private readonly SynchronizeObservable<T> parent;
		}
	}
}
